const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    url:[
        {
          url:'/images/img-01.PNG'
        },{
          url:'/images/img-02.JPG'
        },{
          url:'/images/img-03.JPG'
        }
      ],
    provList:[
        { "name": '推荐'},
        { "name": '名酒名茶'},
        { "name": '新鲜水果'},
				{ "name": '传统美食'},
				{ "name": '腌腊干货'},
				{ "name": '绿色蔬菜'},
				{ "name": '粮油作物'},
				{ "name": '美味湖鲜'},
				{ "name": '特色鲜肉'},
				{ "name": '其它产品'},
    ],
  },


  swichNav: function( e ) {
      var that = this;
      if( this.data.currentTab === e.target.dataset.current ) {
          return false;
      } else {
          that.setData( {
              currentTab: e.target.dataset.current,
              navScrollLeft:e.target.dataset.current >= 4 ? ((e.target.dataset.current) * 60) : 0
          })
      }
  },
  bindChange: function( e ) {
      var that = this;
      that.setData( {
          currentTab: e.detail.currentItemId,
          navScrollLeft:e.detail.current >= 4 ? ((e.detail.current) * 60) : 0
      });
  },
  toSearchPage: function() {
    wx.navigateTo({
      url: '/pages/search-page/search-page',
    })
  },

  //数据过滤
  filter(products){
    let fruits = products.filter((p)=>{
      return p.types[1].indexOf('新鲜水果') !== -1
    })
    let teas = products.filter((p)=>{
      return p.types[1].indexOf('名酒名茶') !== -1
    })
    let food = products.filter((p)=>{
      return p.types[1].indexOf('传统美食') !== -1
    })
    let recommend = products.filter((p)=>{
      return p.types[0] == '地方特产'
    })

    this.setData({
      fruits,
      teas,
      food,
      recommend
    })
  },

  onShow(){
    db.collection('products').get().then(res=>{
      this.filter(res.data)
      this.setData({
        products:res.data
      })
      wx.setStorageSync('products', res.data)
      }).catch(error=>{
        console.log('数据获取失败',error)
      })
  }
})
